package com.whfc.wxmp.api.mach.v1.service.impl;

import com.whfc.common.enums.DeviceType;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.ResultEnum;
import com.whfc.entity.dto.WxDeviceBindCheckDTO;
import com.whfc.fuum.dto.SysMachTypeDTO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.SysMachTypeService;
import com.whfc.hdc.dto.AppDeviceDTO;
import com.whfc.hdc.service.AppDeviceService;
import com.whfc.mach.dto.AppMachDTO;
import com.whfc.mach.param.*;
import com.whfc.mach.service.AppMachDataService;
import com.whfc.mach.service.AppMachService;
import com.whfc.wxmp.api.mach.v1.service.WxmpMachService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author sun_guodong
 * @description
 * @date 2020-11-17
 */
@Service
public class WxmpMachServiceImpl implements WxmpMachService {

    @DubboReference(interfaceClass = SysMachTypeService.class, version = "1.0.0")
    private SysMachTypeService sysMachTypeService;

    @DubboReference(interfaceClass = AppMachDataService.class, version = "1.0.0")
    private AppMachDataService appMachDataService;

    @DubboReference(interfaceClass = AppMachService.class, version = "1.0.0")
    private AppMachService appMachService;

    @DubboReference(interfaceClass = AppDeviceService.class, version = "1.0.0")
    private AppDeviceService commonDeviceService;

    @Override
    public AppMachDTO getMachInfo(Integer machId) {
        AppMachDTO machInfo = appMachDataService.getMachInfo(machId);
        if (machInfo == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "未找到该设备");
        }
        SysMachTypeDTO subMachType = sysMachTypeService.getParentMachTypeById(machInfo.getMachType());
        if (subMachType != null) {
            machInfo.setSubMachTypeName(subMachType.getName());
            machInfo.setSubMachType(subMachType.getMachTypeId());
        }
        return machInfo;
    }

    @Override
    public void add(WxUser wxUser, WxMachAddParam param) {
        String sn = param.getSn();
        // 校验SN
        AppDeviceDTO device = commonDeviceService.getDeviceBySn(sn);
        if (device == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "非法SN");
        }
        if (!DeviceType.mach.getValue().equals(device.getType())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "不属于设备硬件");
        }
        // 添加绑定设备
        param.setDeviceId(device.getId());
        param.setDeviceType(device.getType());
        appMachService.addWxMach(wxUser, param);
    }

    @Override
    public void edit(MachEditParam param) {
        appMachService.edit(param);
    }

    @Override
    public void delMach(Integer machId) {
        appMachService.del(machId);
    }

    @Override
    public void bindDevice(WxUser user, DeviceMachBindParam request) {
        AppDeviceDTO deviceDTO = commonDeviceService.getDeviceBySn(request.getSn());
        if (deviceDTO == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "设备sn码错误");
        }
        if (!DeviceType.mach.getValue().equals(deviceDTO.getType())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "不属于设备硬件");
        }
        request.setDeviceId(deviceDTO.getId());
        request.setUserId(user.getId());
        request.setNickName(user.getNickName());
        request.setDeviceType(deviceDTO.getType());
        appMachService.bindMachDevice(request);
    }

    @Override
    public void unbindDevice(WxUser user, Integer machId) {
        appMachService.unbindMachDevice(user.getId(), user.getNickName(), machId);
    }

    @Override
    public WxDeviceBindCheckDTO bindCheck(String sn) {
        WxDeviceBindCheckDTO result = new WxDeviceBindCheckDTO();
        AppDeviceDTO device = commonDeviceService.getDeviceBySn(sn);
        if (device == null) {
            result.setMsg("未找到该硬件");
            result.setAvailable(0);
            return result;
        }
        if (!DeviceType.mach.getValue().equals(device.getType())) {
            result.setAvailable(0);
            result.setMsg("该硬件不属于设备");
            return result;
        }
        // 校验设备有无绑定
        Integer deviceId = device.getId();
        Boolean b = appMachService.bindCheck(deviceId);
        if (b) {
            result.setMsg("硬件已经被其他人绑定");
            result.setAvailable(0);
            return result;
        }

        result.setAvailable(1);
        result.setMsg("该硬件可用");
        return result;
    }

    @Override
    public void setOilAdd(MachSetOilAddParam param) {
        appMachService.setOilAdd(param);
    }

    @Override
    public ListData<MachOilAdd> getOilAdd(Integer machId, Date date) {
        return appMachService.getOilAdd(machId, date);
    }
}
